import Vue from 'vue'
import Vuex from 'vuex' // uni-app 将vuex内置不需要安装
import { getUserInfo } from "@/common/api/user.js"
import team from "@/common/api/team.js"
import income from "@/common/api/income.js"
const isTeamLeader = team.isTeamLeader
const unReadMsgIncomeExchange = income.unReadMsgIncomeExchange
Vue.use(Vuex)

const store = new Vuex.Store({
	// 数据源
	state:{
		historyList: uni.getStorageSync('__history') || [], // 搜索历史
		userInfo: uni.getStorageSync('LoginInfo') || {}, // 获取用户信息
		headerTabIndex:0,
		msgRead:false,
		canCreateTeam:false,
		isTeamLeader:false,
		userUnReadMsg:uni.getStorageSync('setUserUnReadMsg') || {},
	},
	mutations:{
		SET_USER_INFO(state, userInfo){
			console.log(userInfo)
			state.userInfo = userInfo
		},
		SET_HISTORY_LIST(state, historyList){
			state.historyList = historyList
		},
		SET_CLEAR_HISTORY(state, historyList){
			state.historyList = []
		},
		setHeaderTabIndex(state, val){
			state.headerTabIndex = val
		},
		setMsgRead(state, val){
			state.msgRead = val
		},
		setCanCreateTeam(state, val){
			state.canCreateTeam = val
		},
		setIsTeamLeader(state, val){
			state.isTeamLeader = val
		},
		setUserUnReadMsg(state, val){
			state.userUnReadMsg = val
		},
	},
	getters: {
		loginInfo(state){
			return state.userInfo
		},
		getHeaderTabIndex(state){
			return state.headerTabIndex
		},
		getMsgRead(state){
			return state.msgRead
		},
		getCreateTeam(state){
			return state.canCreateTeam
		},
		getIsTeamLeader(state){
			return state.isTeamLeader
		},
		getUserUnReadMsg(state){
			return state.userUnReadMsg
		},
	},
	actions:{
		// 设置用户信息
		set_userinfo({commit, state}, userInfo){
			return new Promise(reslove => {
				getUserInfo().then(res => {
					if (res.code === 200) {
						
						let LoginInfo = res.data
						commit('SET_USER_INFO', LoginInfo)
						uni.setStorageSync('LoginInfo', LoginInfo)
						
					
					}
					reslove()
				})
			})
		
		},
		// 设置用户未读消息
		setUerUnReadMsg({commit, state}, par){
			return new Promise(reslove => {
				unReadMsgIncomeExchange(par).then(res => {
					if (res.code === 200) {
						let dats = res.data
						commit('setUserUnReadMsg', dats)
						uni.setStorageSync('setUserUnReadMsg', dats)
						
					
					}
					reslove()
				})
			})
		
		},
		set_isTeamLeader({commit, state}, isTl){
			return new Promise(reslove => {
				isTeamLeader().then(res => {
					if (res.code === 200) {
						commit('setIsTeamLeader', res.data.flag)
					}
					reslove()
				})
			})
		
		},
		// 设置搜索历史
		set_history({commit, state}, history){
			console.log(history)
			console.log(state.historyList)
			let flag = true
			for (let i = 0; i < state.historyList.length; i++) {
				if (state.historyList[i].name == history.name) {
					flag = false
				}
			}
			if (flag) {
				let list = state.historyList
				list.unshift(history)
				uni.setStorageSync('__history', list) // 使用uni-app提供的持久化操作
				commit('SET_HISTORY_LIST', list)
			}
		},
		// 情况搜索数据
		clear_history({commit, state}, history){
			// uni.setStorageSync('__history', [])
			uni.clearStorageSync('__history') // 清除本地缓存
			commit('SET_CLEAR_HISTORY')
		},
		// 设置用户信息
	}
})
export default store